package com.learn.zq.consulconsumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author : Zhang Qi
 * @date : 2019/10/9
 */
@RestController
public class CallHelloController {
  @Autowired
  private LoadBalancerClient loadBalancerClient;
  @GetMapping("/call")
  public String call(){
    ServiceInstance serviceInstance = loadBalancerClient.choose("service-producer");
    StringBuilder sb=new StringBuilder();
    sb.append("服务地址：").append(serviceInstance.getUri()).append("\n");
    sb.append("服务名称：").append(serviceInstance.getServiceId()).append("\n");
    String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
    sb.append(callServiceResult);
    return sb.toString();
  }
}
